﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class UCQuanTri : UserControl
    {
        public UCQuanTri()
        {
            InitializeComponent();
        }
        private void UCQuanTri_Load(object sender, EventArgs e)
        {
            cmbChucVuThem.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVuThem.DisplayMember = "TenChucVu";
            cmbChucVuThem.ValueMember = "MaCV";

            cmbChucVu.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVu.DisplayMember = "TenChucVu";
            cmbChucVu.ValueMember = "MaCV";

            dgvNguoiDung_Load();

        }

        private void dgvNguoiDung_Load()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.AliceBlue;
            style.BackColor = Color.Red;

            List<NguoiDungQHPublic> list_nd = NguoiDungQHBLL.LoadToanBoNguoiDung();
           
            dgvNguoiDung.DataSource = list_nd;

            dgvNguoiDung.Columns[0].HeaderText = "Mã người dùng";
            //dgvNguoiDung.Columns[1].HeaderText = "Mã nhân khẩu";
            dgvNguoiDung.Columns[2].HeaderText = "Tên đăng nhập";
            dgvNguoiDung.Columns[3].HeaderText = "Mật khẩu";
            dgvNguoiDung.Columns[4].HeaderText = "Họ Tên";
            dgvNguoiDung.Columns[5].HeaderText = "Chức vụ";

            dgvNguoiDung.Columns[6].HeaderText = "Tình trạng";
            dgvNguoiDung.Columns[7].HeaderText = "Ngày sinh";
            dgvNguoiDung.Columns[8].HeaderText = "Bắt đầu";
            dgvNguoiDung.Columns[9].HeaderText = "Kết thúc";
            
            //dgvNguoiDung.Columns[0].Visible = false;
            dgvNguoiDung.Columns[1].Visible = false;
            //dgvNguoiDung.Columns[2].Visible = false;
            dgvNguoiDung.Columns[3].Visible = false;
            //dgvNguoiDung.Columns[4].Visible = false;
            //dgvNguoiDung.Columns[5].Visible = false;
            //txtMaNKThem.Text = DungChung.LoadMaND().ToString();
            dgvNguoiDung.Columns[6].Visible = false;

            for (int i = dgvNguoiDung.RowCount - 1; i >= 0; i--)
            {
                if ((bool)dgvNguoiDung.Rows[i].Cells[6].Value == false)
                {
                    dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                }
            }

        }
        public int mank;
        public int mand;
        private void dgvNguoiDung_SelectionChanged(object sender, EventArgs e)
        {
           
            DataGridViewCell cell = null;
            foreach (DataGridViewCell select in dgvNguoiDung.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                //row.Cells[0].Value.ToString();
                mand = int.Parse(row.Cells[0].Value.ToString());
                txtMaNK.Text = (string)row.Cells[1].Value.ToString();
                mank = (int)row.Cells[1].Value;
                txtTenDangNhap.Text = row.Cells[2].Value.ToString();
                dateNgaySinhCu.Value = (DateTime)row.Cells[7].Value;
                dateBatDauCu.Value = (DateTime)row.Cells[8].Value;
                dateKetThucCu.Value = (DateTime)row.Cells[9].Value;
                txtHoTen.Text = (string)row.Cells[4].Value;
                //cmbChucVu.Text = (ChucVuBLL.LoadChucVuByMaCV((int)row.Cells[5].Value).TenChucVu).ToString();
                cmbChucVu.SelectedIndex = int.Parse(row.Cells[5].Value.ToString())-1;
                //if ((int)cmbChucVu.SelectedValue == (int)row.Cells[4].Value)
                //{ 
                //    cmbChucVu.SelectedValue==
                //}
                //.Text = (string)row.Cells[4].Value;
                if ((bool)row.Cells[6].Value == true)
                {
                    //labTinhTrang.Text = "Đang hoạt động";
                    sbtTinhTrang.Value = true;
                }
                else
                {
                    //labTinhTrang.Text = "Ngừng hoạt động";
                    sbtTinhTrang.Value = false;
                }
            }

        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            if (txtHoTenThem.Text == "" && txtMaNKThem.Text==""|| txtTenDangNhapThem.Text == "")
            {
                MessageBox.Show("Nhập đầy đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtMaNKThem.Focus();
                return;
            }
            if (dateBatDau.Value > dateKetThuc.Value)
            {
                MessageBox.Show("Nhiệm kì không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dateBatDau.Focus();
                return;
            }
            if (dateNgaySinh.Value > DateTime.Now)
            {
                MessageBox.Show("Ngày sinh không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dateNgaySinh.Focus();
                return;
            }
            NguoiDungQHPublic nd = new NguoiDungQHPublic();
            nd.MaND = NguoiDungBLL.LoadMaND() + 1;
            
            nd.HoTen = txtHoTenThem.Text;
            nd.MatKhau = DungChung.md5(dateNgaySinh.Value.Day.ToString()+dateNgaySinh.Value.Month.ToString()+dateNgaySinh.Value.Year.ToString());
            nd.TenDangNhap = txtTenDangNhapThem.Text;
            nd.MaCV = (byte)cmbChucVuThem.SelectedValue;
            nd.NgaySinh = dateNgaySinh.Value;
            nd.ThoiGianBatDau = dateBatDau.Value;
            nd.ThoiGianKetThuc = dateKetThuc.Value;
            if (DateTime.Now <= dateKetThuc.Value)
                nd.TinhTrang = true;
            else
                nd.TinhTrang = false;
            if (txtMaNKThem.Text != "")
            {
                nd.MaNK = int.Parse(txtMaNKThem.Text);
                NguoiDungQHBLL.InsertNguoiDungNK(nd);
            }
            else
            {
                NguoiDungQHBLL.InsertNguoiDung(nd);
            }
            MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            UCQuanTri_Load(sender, e);
        }

       
        private void btnLuu_Click(object sender, EventArgs e)
        {
            //NguoiDungBLL.UpdateTinhTrang(mand, sbtTinhTrang.Value);
            NguoiDungQHBLL.UpdateNguoiDung(mand, txtTenDangNhap.Text, txtHoTen.Text, DungChung.md5(dateNgaySinh.Value.Day.ToString() + dateNgaySinh.Value.Month.ToString() + dateNgaySinh.Value.Year.ToString()), dateNgaySinhCu.Value, dateBatDauCu.Value, dateKetThucCu.Value, (bool)sbtTinhTrang.Value, (byte)cmbChucVu.SelectedValue);
            MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            UCQuanTri_Load(sender, e);

            //btnTim_Click(sender, e);
        }
        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            
            if (mank == 0)
                MessageBox.Show("Không có hồ sơ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                frmNhanKhau nk = new frmNhanKhau(mank, mand);
                nk.ShowDialog();
            }
        }

        private void txtMaNKThem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtMaNKThem.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtMaNK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtMaNKThem.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtMaNK_Leave(object sender, EventArgs e)
        {
            if (txtMaNK.Text == "")
                return;
            NhanKhauPublic nk= NhanKhauBLL.LoadNhanKhauByMaNK(int.Parse(txtMaNK.Text));
            if (nk == null)
            {
                txtMaNK.Text = string.Empty;
                txtHoTen.Enabled = true;
                dateNgaySinhCu.Enabled = true;
               
            }
            else
            {
                txtHoTen.Text = nk.HoTen;
                dateNgaySinhCu.Value = (DateTime)nk.NgaySinh;
                dateNgaySinhCu.Enabled = false;
                txtHoTen.Enabled = false;
            }
        }

        private void txtMaNKThem_Leave(object sender, EventArgs e)
        {
            if (txtMaNKThem.Text == "")
                return;
            NhanKhauPublic nk = NhanKhauBLL.LoadNhanKhauByMaNK(int.Parse(txtMaNKThem.Text));
            if (nk == null)
            {
                txtMaNKThem.Text = string.Empty;
                txtHoTenThem.Enabled = true;
                dateNgaySinh.Enabled = true;
                txtHoTenThem.ResetText();
                dateNgaySinh.Refresh();
            }
            else
            {
                txtHoTenThem.Text = nk.HoTen;
                dateNgaySinh.Value = (DateTime)nk.NgaySinh;
                dateNgaySinh.Enabled = false;
                txtHoTenThem.Enabled = false;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (frmDangNhap.user_login.MaND == mand)
            {
                MessageBox.Show("Không thể xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (DialogResult.OK == result)
            {
                NguoiDungQHBLL.DeleteNguoiDung(mand);
                UCQuanTri_Load(sender, e);
            }
        }
    }
}
